/**
 *    Copyright 2010 Merrick Zhang ( anphorea@gmail.com )
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 */
package anpho.proxyvalidator.bean;

import java.net.Proxy;

/**
 * @author Merrick
 */
public class ProxyBean {

	public Proxy	_proxy;
	public long[]	respTimes;

	/**
	 * set the proxy & count.
	 */
	public ProxyBean(Proxy p, int count) {
		this._proxy = p;
		respTimes = new long[count];
	}

	/**
	 * get the average response time of this Proxy.the average response time is
	 * calculated ignoring TIMEOUT values and UNACCESSABLE values.
	 * 
	 * @return 0: if no valid values ; otherwise: the average value of the
	 *         response times.
	 */
	public long getAvgTime() {
		long l = 0L;
		int c = 1;
		for (long t : respTimes) {
			if (t != IConstants.TIMEOUT && t != IConstants.UNACCESSABLE) {
				l += t;
				c++;
			}
		}
		return l / c;
	}

	/**
	 * get the failure rate of the current Proxy.this value is useful for
	 * determine whether a proxy is stable.
	 * 
	 * @return [0.00 ~ 1.00] returning 1.00 means all the results are TIMEOUT or
	 *         UNACCESSABLE as the proxy is totally useless.
	 */
	public double getFailRate() {
		if (respTimes.length == 0)
			return 1;

		float f = 0;
		for (long t : respTimes) {
			if (t == IConstants.TIMEOUT || t == IConstants.UNACCESSABLE) {
				f++;
			}
		}
		return f / respTimes.length;
	}
}
